package com.rentalcar.rental_car.utils;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class MD5 {
    public static String encoding(String password){
        try {
            //获取信息摘要器
            MessageDigest digest = MessageDigest.getInstance("md5");
            byte[] res = digest.digest(password.getBytes());
            StringBuffer buffer = new StringBuffer();

            for (byte b : res) {
                int number = b & 0xff;  //加盐
                String s = Integer.toHexString(number); //转化为16进制
                if (s.length() == 1){
                    buffer.append("0");
                }
                buffer.append(s);
            }
            return buffer.toString();
        }
        catch (NoSuchAlgorithmException e){
            e.printStackTrace();
            return null;
        }
    }
}
